home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / sdcte.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  6.1 KB  |  206 lines

  1. /* Copyright (C) 1994, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: sdcte.c,v 1.2 2000/09/19 19:00:49 lpd Exp $ */
  20. /* DCT encoding filter stream */
  21. #include "memory_.h"
  22. #include "stdio_.h"
  23. #include "jpeglib_.h"
  24. #include "jerror_.h"
  25. #include "gdebug.h"
  26. #include "gsmemory.h"        /* for gsmalloc.h */
  27. #include "gsmalloc.h"        /* for gs_memory_default */
  28. #include "strimpl.h"
  29. #include "sdct.h"
  30. #include "sjpeg.h"
  31.  
  32. /* ------ DCTEncode ------ */
  33.  
  34. /* JPEG destination manager procedures */
  35. private void
  36. dcte_init_destination(j_compress_ptr cinfo)
  37. {
  38. }
  39. private boolean
  40. dcte_empty_output_buffer(j_compress_ptr cinfo)
  41. {
  42.     return FALSE;
  43. }
  44. private void
  45. dcte_term_destination(j_compress_ptr cinfo)
  46. {
  47. }
  48.  
  49. /* Set the defaults for the DCTEncode filter. */
  50. private void
  51. s_DCTE_set_defaults(stream_state * st)
  52. {
  53.     stream_DCT_state *const ss = (stream_DCT_state *) st;
  54.  
  55.     s_DCT_set_defaults(st);
  56.     ss->QFactor = 1.0;
  57.     ss->ColorTransform = 0;
  58.     ss->Markers.data = 0;
  59.     ss->Markers.size = 0;
  60.     ss->NoMarker = true;
  61. }
  62.  
  63. /* Initialize DCTEncode filter */
  64. private int
  65. s_DCTE_init(stream_state * st)
  66. {
  67.     stream_DCT_state *const ss = (stream_DCT_state *) st;
  68.     struct jpeg_destination_mgr *dest = &ss->data.compress->destination;
  69.  
  70.     dest->init_destination = dcte_init_destination;
  71.     dest->empty_output_buffer = dcte_empty_output_buffer;
  72.     dest->term_destination = dcte_term_destination;
  73.     ss->data.common->memory = ss->jpeg_memory;
  74.     ss->data.compress->cinfo.dest = dest;
  75.     ss->phase = 0;
  76.     return 0;
  77. }
  78.  
  79. /* Process a buffer */
  80. private int
  81. s_DCTE_process(stream_state * st, stream_cursor_read * pr,
  82.            stream_cursor_write * pw, bool last)
  83. {
  84.     stream_DCT_state *const ss = (stream_DCT_state *) st;
  85.     jpeg_compress_data *jcdp = ss->data.compress;
  86.     struct jpeg_destination_mgr *dest = jcdp->cinfo.dest;
  87.  
  88.     dest->next_output_byte = pw->ptr + 1;
  89.     dest->free_in_buffer = pw->limit - pw->ptr;
  90.     switch (ss->phase) {
  91.     case 0:        /* not initialized yet */
  92.         if (gs_jpeg_start_compress(ss, TRUE) < 0)
  93.         return ERRC;
  94.         pw->ptr = dest->next_output_byte - 1;
  95.         ss->phase = 1;
  96.         /* falls through */
  97.     case 1:        /* initialized, Markers not written */
  98.         if (pw->limit - pw->ptr < ss->Markers.size)
  99.         return 1;
  100.         memcpy(pw->ptr + 1, ss->Markers.data, ss->Markers.size);
  101.         pw->ptr += ss->Markers.size;
  102.         ss->phase = 2;
  103.         /* falls through */
  104.     case 2:        /* still need to write Adobe marker */
  105.         if (!ss->NoMarker) {
  106.         static const byte Adobe[] =
  107.         {
  108.             0xFF, JPEG_APP0 + 14, 0, 14,    /* parameter length */
  109.             'A', 'd', 'o', 'b', 'e',
  110.             0, 100,    /* Version */
  111.             0, 0,    /* Flags0 */
  112.             0, 0,    /* Flags1 */
  113.             0        /* ColorTransform */
  114.         };
  115.  
  116. #define ADOBE_MARKER_LEN sizeof(Adobe)
  117.         if (pw->limit - pw->ptr < ADOBE_MARKER_LEN)
  118.             return 1;
  119.         memcpy(pw->ptr + 1, Adobe, ADOBE_MARKER_LEN);
  120.         pw->ptr += ADOBE_MARKER_LEN;
  121.         *pw->ptr = ss->ColorTransform;
  122. #undef ADOBE_MARKER_LEN
  123.         }
  124.         dest->next_output_byte = pw->ptr + 1;
  125.         dest->free_in_buffer = pw->limit - pw->ptr;
  126.         ss->phase = 3;
  127.         /* falls through */
  128.     case 3:        /* markers written, processing data */
  129.         while (jcdp->cinfo.image_height > jcdp->cinfo.next_scanline) {
  130.         int written;
  131.  
  132.         /*
  133.          * The data argument for jpeg_write_scanlines is
  134.          * declared as a JSAMPARRAY.  There is no corresponding
  135.          * const type, so we must remove const from the
  136.          * argument that we are passing here.  (Tom Lane of IJG
  137.          * judges that providing const analogues of the
  138.          * interface types wouldn't be worth the trouble.)
  139.          */
  140.         /*const */ byte *samples = (byte *) (pr->ptr + 1);
  141.  
  142.         if ((uint) (pr->limit - pr->ptr) < ss->scan_line_size) {
  143.             if (last)
  144.             return ERRC;    /* premature EOD */
  145.             return 0;    /* need more data */
  146.         }
  147.         written = gs_jpeg_write_scanlines(ss, &samples, 1);
  148.         if (written < 0)
  149.             return ERRC;
  150.         pw->ptr = dest->next_output_byte - 1;
  151.         if (!written)
  152.             return 1;    /* output full */
  153.         pr->ptr += ss->scan_line_size;
  154.         }
  155.         ss->phase = 4;
  156.         /* falls through */
  157.     case 4:        /* all data processed, finishing */
  158.         /* jpeg_finish_compress can't suspend, so write its output
  159.          * to a fixed-size internal buffer.
  160.          */
  161.         dest->next_output_byte = jcdp->finish_compress_buf;
  162.         dest->free_in_buffer = sizeof(jcdp->finish_compress_buf);
  163.         if (gs_jpeg_finish_compress(ss) < 0)
  164.         return ERRC;
  165.         jcdp->fcb_size =
  166.         dest->next_output_byte - jcdp->finish_compress_buf;
  167.         jcdp->fcb_pos = 0;
  168.         ss->phase = 5;
  169.         /* falls through */
  170.     case 5:        /* copy the final data to the output */
  171.         if (jcdp->fcb_pos < jcdp->fcb_size) {
  172.         int count = min(jcdp->fcb_size - jcdp->fcb_pos,
  173.                 pw->limit - pw->ptr);
  174.  
  175.         memcpy(pw->ptr + 1, jcdp->finish_compress_buf + jcdp->fcb_pos,
  176.                count);
  177.         jcdp->fcb_pos += count;
  178.         pw->ptr += count;
  179.         if (jcdp->fcb_pos < jcdp->fcb_size)
  180.             return 1;
  181.         }
  182.         return EOFC;
  183.     }
  184.     /* Default case can't happen.... */
  185.     return ERRC;
  186. }
  187.  
  188. /* Release the stream */
  189. private void
  190. s_DCTE_release(stream_state * st)
  191. {
  192.     stream_DCT_state *const ss = (stream_DCT_state *) st;
  193.  
  194.     gs_jpeg_destroy(ss);
  195.     gs_free_object(ss->data.common->memory, ss->data.compress,
  196.            "s_DCTE_release");
  197.     /* Switch the template pointer back in case we still need it. */
  198.     st->template = &s_DCTE_template;
  199. }
  200.  
  201. /* Stream template */
  202. const stream_template s_DCTE_template =
  203. {&st_DCT_state, s_DCTE_init, s_DCTE_process, 1000, 4000, s_DCTE_release,
  204.  s_DCTE_set_defaults
  205. };
  206.